package com.eagle.tool;

import com.eagle.tool.MapTest1;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 跟案例1{@link MapTest1}类似，传统的方法写还是冗长，可读性不高。
 *
 * @author qinlinsen
 */
public class FilterTest2 {
    private static final List<String> technicalExperts = Arrays.asList("Dave Syer",
            "Elon Musk",
            "Abraham Lincoln",
            "Josh Bloch",
            "Mark Reinhold",
            "John Rodson",
            "Gavin King",
            "Doug Lea");

    @Test
    public void testUseFunctionalWay() {
        //选出所有以字母D开始的技术专家：
        final List<String> expertNameStartWithLetterD = technicalExperts.stream()
                .filter(expertName -> expertName.startsWith("D"))
                .collect(Collectors.toList());
        //遍历所有以字母D开始的技术专家
        System.out.println("----------------开始遍历以字母D开始的技术专家---------------------");
        expertNameStartWithLetterD.forEach(System.out::println);
        System.out.println("----------------结束遍历以字母D开始的技术专家---------------------");

    }


    @Test
    public void testTraditionWay() {
        final List<String> expertNameStartWithLetterD = new ArrayList<>();
        for (String expertName : technicalExperts) {
            if (expertName.startsWith("D")) {
                expertNameStartWithLetterD.add(expertName);
            }
        }

        //遍历所有以字母D开始的技术专家
        System.out.println("----------------开始遍历以字母D开始的技术专家---------------------");
        for (String expertName : expertNameStartWithLetterD) {
            System.out.println(expertName);
        }
        System.out.println("----------------结束遍历以字母D开始的技术专家---------------------");


    }

}
